package com.lzx.lizhenxing.user.consume;

import com.lzx.lizhenxing.common.config.RabbitMQConfig;
import com.lzx.lizhenxing.user.dao.UserOrderDao;
import com.lzx.lizhenxing.user.model.UserOrderModel;
import com.lzx.lizhenxing.user.service.UserExpressDeliveryService;
import com.lzx.lizhenxing.user.service.UserOrderService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Component
public class OrderDeadConsumer {

    @Resource
    private UserOrderDao userOrderDao;

    @Resource
    private UserExpressDeliveryService userExpressDeliveryService;

    @RabbitListener(queues = RabbitMQConfig.ORDER_DEAD_QUEUE)
    public void handleDeadMessage(Long orderId) {
        // 1. 查询订单状态
        UserOrderModel order = userOrderDao.selectById(orderId);
        if (Objects.isNull(order)) {
            return;
        }
        // 2. 如果订单未支付，关闭订单
        if (Objects.equals(order.getStatus(),0)) {
            userOrderDao.deleteById(orderId);
            // 同时快递的状态设置为已到达1
            userExpressDeliveryService.setExpressDeliveryStatus(order.getExpressDeliveryId(),1);
            System.out.println("关闭订单：" + orderId);
        }
    }
}